package com.grape.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.grape.constants.Constants;
import com.grape.utils.ThreadLocalUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
 * 公共实体基类
 * @author  putao
 * @email   2628279194@qq.com
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BaseDomain extends Base {

    /**
     * 删除标记 0：正常，1：删除
     */
    @ApiModelProperty("删除标记")
    private String delFlag = BaseDomain.DEL_FLAG_NORMAL;

    /**
     * 创建时间
     */
    @ApiModelProperty("创建时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date createTime;

    /**
     * 新增人id
     */
    @ApiModelProperty("新增人id")
    private String createId;

    /**
     * 修改人id
     */
    @ApiModelProperty("修改人id")
    private String modifyId;

    /**
     * 修改时间
     */
    @ApiModelProperty("修改时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date modifyTime;

    public static String DEL_FLAG_DELETE = "1";
    public static String DEL_FLAG_NORMAL = "0";

    /**
     * 新增初始化
     */
    public void insertInit() {
        super.insertInit();
        this.delFlag = Constants.STATUS_NORMAL;
        this.createTime = new Date();
        this.createId = ThreadLocalUtil.getUserId();
        this.modifyId = ThreadLocalUtil.getUserId();
        this.modifyTime = new Date();
    }


    /**
     * 新增初始化
     * @param id    主键id
     */
    public void insertInit(String id) {
        super.insertInit(id);
        this.delFlag = Constants.STATUS_NORMAL;
        this.createTime = new Date();
        this.createId = ThreadLocalUtil.getUserId();
        this.modifyId = ThreadLocalUtil.getUserId();
        this.modifyTime = new Date();
    }

    /**
     * 新增初始化
     * @param userId    用户id
     */
    public void insertInitByUseId(String userId) {
        super.insertInit();
        this.delFlag = Constants.STATUS_NORMAL;
        this.createTime = new Date();
        this.createId = userId;
        this.modifyId = userId;
        this.modifyTime = new Date();
    }

    /**
     * 修改初始化
     */
    public void updateInit() {
        this.modifyId = ThreadLocalUtil.getUserId();
        this.modifyTime = new Date();
    }

 }
